package com.kinon.bs_app.activities;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kinon.bs_app.R;
import com.kinon.bs_app.utils.BaseActivity;
import com.kinon.bs_app.utils.HttpUtils;
import com.kinon.bs_app.utils.PrefUtils;
import com.kinon.bs_app.utils.http.StrCallback;

import retrofit2.Call;
import retrofit2.Response;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_phone = null;
    private EditText et_password = null;

    private Button btn_login = null;
    private Button btn_register = null;

    private TextView tv_forgetPw = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initListener();
        initPref();
    }

    private void initView() {
        et_phone = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_password);

        btn_login = findViewById(R.id.btn_login);
        btn_register = findViewById(R.id.btn_register);

        tv_forgetPw = findViewById(R.id.tv_forgetPw);
    }

    private void initListener() {
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        tv_forgetPw.setOnClickListener(this);
    }

    private void initPref() {
        PrefUtils.init(LoginActivity.this);

        String p1 = PrefUtils.getString("phone", null);
        String p2 = PrefUtils.getString("password", null);

        if (p1 != null) {
            et_phone.setText(p1);
            if (p2 != null) {
                et_password.setText(p2);
                login();
            }
        }
    }

    private void login() {
        final String phone = et_phone.getText().toString().trim();
        final String password = et_password.getText().toString().trim();

        if (phone.isEmpty()) {
            showToast("手机号不能为空");
            return;
        }
        if (password.isEmpty()) {
            showToast("密码不能为空");
            return;
        }

        Call<String> call = HttpUtils.getRequestApi().login(phone, password, 2+"", Build.MODEL);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Log.d("zzy", "token: "+data);

                PrefUtils.putString("token", data);
                PrefUtils.putString("phone", phone);
                PrefUtils.putString("password", password);

                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onError(int code, String error) {
                Log.e("zzy", "error: "+error);
                showToast(error);
            }
        });
    }

    private void register() {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }

    private void forgetPw() {
        Intent intent = new Intent(LoginActivity.this, ForgetPwActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.btn_register:
                register();
                break;
            case R.id.tv_forgetPw:
                forgetPw();
                break;
            default:
                break;
        }
    }
}
